﻿<%@ WebHandler Language="C#" Class="SetBool" %>

using System;
using System.Web;
using Etong.Web;
public class SetBool : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string tab = context.Request.QueryString["tab"];
        string xml = context.Request.QueryString["xml"];
        string col = context.Request.QueryString["col"];
        string id = context.Request.QueryString["id"];
        ConfigTable cTab = ConfigTable.GetConfigTable(tab, xml);
        ConfigColumns cCol = cTab[col];
        string sqlGet = "select " + col + " from " + tab + " where " + cTab.PkFiledName + "=" + DataTypeFactory.GetSqlValue(id, cTab[cTab.PkFiledName].ColumnDataType);
        string OldValue = DBFactory.GetConn().exe1(sqlGet);

        bool oldBool = false;
        bool.TryParse(OldValue, out oldBool);

        bool newBool = !oldBool;

        string sqlSetDate = string.Empty;
        if (!string.IsNullOrEmpty(cCol.ListShowBoolLinkDateColumnName))
        {
            //判断在配置文件中是否有此时间字段。因配置文件不一定和最新的数据库保持同步更新，所以有可能导致数据库中有此字段而配置文件中没有，时间更新失败，注释掉if则不判断。            
            //if (cTab[cCol.ListShowBoolLinkDateColumnName] != null)
            {
                sqlSetDate = ","+cCol.ListShowBoolLinkDateColumnName+"=";
                sqlSetDate += (newBool) ? DataTypeFactory.GetSqlValue(DateTime.Now.ToString(), ColumnDataType.DateTime) : "null";
            }
        }

        string updateSql = "update " + tab + " set " + col + "=" + DataTypeFactory.GetSqlValue(newBool.ToString(), ColumnDataType.Bool) + sqlSetDate+ " where " + cTab.PkFiledName + "=" + DataTypeFactory.GetSqlValue(id, cTab[cTab.PkFiledName].ColumnDataType);

        context.Response.Write(updateSql);
        DBFactory.GetConn().exe0(updateSql);

        context.Response.Redirect(context.Request.UrlReferrer.ToString());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}